package com.enjoyor.soa.traffic.restt.tpcms.dubbo.impl;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.dto.tpcms.ApplyZoneDto;
import com.enjoyor.soa.traffic.api.dto.tpcms.InfoZoneDto;
import com.enjoyor.soa.traffic.api.service.dubbo.tpcms.IDubboInfoZoneService;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.IInfoZoneConsumeService;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import oracle.spatial.geometry.JGeometry;

@Component
public class InfoZoneConsumeService implements IInfoZoneConsumeService {

    @Reference(version = "1.0.0", timeout = 10000)
    private IDubboInfoZoneService dubboInfoZoneService;

    /**
     * 获取区域分页信息
     */
    @SuppressWarnings("rawtypes")
    @Override
    public ResultPojo getInfoZonePage(Page page) {
        return dubboInfoZoneService.getInfoZonePage(page);
    }

    /**
     * 获取区域信息列表
     */
    @Override
    public ResultPojo getInfoZoneList(String applyZoneId, String applyZoneName, String zoneTypeId, String isUse) {
        return dubboInfoZoneService.getInfoZoneList(applyZoneId, applyZoneName, zoneTypeId, isUse);
    }

    /**
     * 新增区域信息
     */
    @Override
    public ResultPojo insertInfoZone(ApplyZoneDto applyZoneDto, String userName) {
        return dubboInfoZoneService.insertInfoZone(applyZoneDto, userName);
    }

    /**
     * 修改区域信息
     */
    @Override
    public ResultPojo editInfoZone(ApplyZoneDto applyZoneDto, String userName) {
        return dubboInfoZoneService.editInfoZone(applyZoneDto, userName);
    }

    /**
     * 删除区域信息
     */
    @Override
    public ResultPojo deleteInfoZone(String infoZoneId, String userName) {
        return dubboInfoZoneService.deleteInfoZone(infoZoneId, userName);
    }

    /**
     * 绘制地图区域
     */
    @Override
    public ResultPojo drawingZoneShape(InfoZoneDto infoZone, String geom, String userName) {
        JGeometry jGeom = null;
        // 将信息字符串转换为对象
        if (!geom.equals("")) {
            jGeom = GeomtryHelper.getJTring2Geom(geom);
        }
        // 因为Json规范无法传入Dubbo.NaN,以下操作修改GsonBuilder.serializeSpecialFloatingPointValues的默认值,避免NaN报错
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.serializeSpecialFloatingPointValues();
        Gson gson = gsonBuilder.create();
        String jGeometryJson = gson.toJson(jGeom);
        JGeometry fromJson = gson.fromJson(jGeometryJson, JGeometry.class);

        infoZone.setZoneGeo(fromJson);
        return dubboInfoZoneService.drawingZoneShape(infoZone, userName);
    }

}
